package datastructure;

import java.io.Serializable;


//Struktur data untuk pair sama dengan c++
public class Pair<U, V> implements Comparable<Pair<U,V>> , Serializable {

  public final U first;
  public final V second;
  
  public static<U,V> Pair<U,V> makePair(U first, V second) {
    return new Pair<U,V>(first, second);
  }
  
  private Pair(U first, V second) {
    this.first = first;
    this.second = second;
  }
  
  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    } 
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    
    Pair pair = (Pair)o;
    
    return !(first != null ? !first.equals(pair.first) : pair.first != null) && !(second != null ? !second.equals(pair.second) : pair.second != null) ;
  }

  @Override
  public int hashCode() {
    int hash = 5;
    hash = 37 * hash + (this.first != null ? this.first.hashCode() : 0);
    hash = 37 * hash + (this.second != null ? this.second.hashCode() : 0);
    return hash;
  }
  
  @Override
  public String toString() {
    return "(" + first + "," + second + ")";
  }
  
  @Override
  public int compareTo(Pair<U, V> o) {
    int value = ((Comparable<U>)first).compareTo(o.first);
    if (value != 0) {
      return value;
    }
    return ((Comparable<V>)second).compareTo(o.second);
  }
  
}
